package models.ticket;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import models.User;
import play.db.ebean.Model;

/*
 * Model ticket
 * QnA for users
 * Author: Erasyl Dabyssov
 */

@Entity
public class Ticket extends Model {
	
	@Id
	public Long id;
	
	public String title;
	
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	public TicketCategory category;
	
	@OneToMany(mappedBy = "ticket", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	public User user;
	
	@OneToMany(mappedBy = "ticket", cascade = {CascadeType.ALL})
	public List<TicketMessage> messages;	
	
	public Boolean resolved; // { 0 => false; 1 => true}
	
	public static Finder<Long, Ticket> find = new Finder<Long, Ticket>(Long.class, Ticket.class);
	
	public static Ticket create(String title, TicketCategory category, User user, TicketMessage message)
	{
		Ticket ticket = new Ticket();
		ticket.category = category;
		ticket.user = user;
		ticket.messages.add(message);
		ticket.resolved = false;
		
		return ticket;
	}
}